using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using XML;

public class TextureManager {

    private static TextureManager instance;

    
    public Dictionary<string, Texture> listTextures;
    public Dictionary<string, ButtonTexture> listButtonTextures;
	
	// Use this for initialization
    private TextureManager()
    {
        listTextures = new Dictionary<string, Texture>();
        listButtonTextures = new Dictionary<string, ButtonTexture>();
        LoadTextures();
	}
	
	public static TextureManager Instance(){
		
			if(instance == null)
				instance = new TextureManager();	
			
			
			return instance;
		
	}

    public Texture GetTexture(string name)
    {
        if(!listTextures.ContainsKey(name))
		{
            Debug.Log(name + " texture not load");
			return null;
		}

        return listTextures[name];
    }

    public ButtonTexture GetButtonTexture(string name)
    {
        if (!listButtonTextures.ContainsKey(name))
            Debug.Log(name + " texture not found");
        return listButtonTextures[name];
    }


    void LoadTextures()
    {
        DeserializeTextureFile(Const.PATH_XML + "textures");
		DeserializeButtonTextureFile(Const.PATH_XML + "texturesButtons");
    }

    //Deserialize a file
    void DeserializeTextureFile(string path)
    {
		
		TextAsset txt = (TextAsset)Resources.Load(path);
		
		InStream stream = new InStream(txt.text);
		
		string name;
		string texture = "";
		
		
		//Read the xml file
		stream.List("texture", delegate(InStream stream2)
        {
			stream2.Attribute("name", out name);
            stream2.Attribute("res", out texture);


            //Load with good resolution
            listTextures.Add(name, LoadTextureWithResolution(texture));
            

		});

    }


    //Deserialize a button file
    void DeserializeButtonTextureFile(string path)
    {
        
        TextAsset txt = (TextAsset)Resources.Load(path);

        InStream stream = new InStream(txt.text);
        
        string name;
             

        //Read the xml file
        stream.List("texture", delegate(InStream stream2)
        {
            ButtonTexture button = new ButtonTexture();

            stream2.Attribute("name", out name);
            stream2.List("normal", delegate(InStream stream3)
            {
                SetValuesWithAttributes(button, name, stream3, ButtonTexture.StateButton.NORMAL);
            });

            stream2.List("over", delegate(InStream stream3)
            {
                SetValuesWithAttributes(button, name, stream3, ButtonTexture.StateButton.OVER);
            });

            stream2.List("release", delegate(InStream stream3)
            {
                SetValuesWithAttributes(button, name, stream3, ButtonTexture.StateButton.RELEASE);
            });

            stream2.List("lock", delegate(InStream stream3)
            {
                SetValuesWithAttributes(button, name, stream3, ButtonTexture.StateButton.LOCK);
            });

            stream2.List("unlock", delegate(InStream stream3)
            {
                SetValuesWithAttributes(button, name, stream3, ButtonTexture.StateButton.UNLOCK);
            });

            stream2.List("click", delegate(InStream stream3)
            {
                SetValuesWithAttributes(button, name, stream3, ButtonTexture.StateButton.CLICK);
            });         
			
			stream2.List("baseButton", delegate(InStream stream3)
			{
				stream3.Content(out button.baseButton);	
			});

            listButtonTextures.Add(name, button);

        });
         
    }

    //Set all value of buttoninfo with the state
    void SetValuesWithAttributes(ButtonTexture button, string name, InStream stream, ButtonTexture.StateButton state)
    {
        string textureName = "";
        int nbAnim = 0;
        bool loop = false;
        bool nextWait = false;

        //Get Button state information
        stream.Attribute("res", out textureName);
        stream.Attribute("nbAnim", out nbAnim);
        stream.Attribute("loop", out loop);
        stream.Attribute("nextWait", out nextWait);

        //Load Texture
        Texture texture = LoadTextureWithResolution(textureName);

        //Create Button Info and add to list
        ButtonInfo info = new ButtonInfo(name, texture, nbAnim, loop, nextWait);
        button.AddToList(state, info);
        
    }


    //Load texture at good resolution for device
    Texture LoadTextureWithResolution(string path, bool useDefaultResolution = false)
    {

        return Resources.Load(Const.PATH_TEXTURE+ path) as Texture;

    }

    
}
